import axios from 'axios'
import { Connect } from './apiUrl'
import {vv} from '../main'
// import store from '../vuex/index'
let qs = require('qs')
var baseURL = Connect.baseURL
// 请求带cookie
axios.defaults.withCredentials = true

//=============================================================普通form表单携带Token提交👇==================================================//
// form提交
let formTokenPost = axios.create({
  baseURL,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    'Accept-Language': 'zH-CN',
  },
  method: 'post',
})

// 请求拦截 塞入Token
formTokenPost.interceptors.request.use(config => {
  // console.log( 'token',localStorage.getItem('accessToken'));
  if (localStorage.getItem('accessToken')) {
    config.headers.token = localStorage.getItem('accessToken')
  } else {
    this.$message.error('网络出错，请重新登录')
    this.$router.push('/')
  }
  return config
})

// 响应拦截
formTokenPost.interceptors.response.use(
  function(response) {
    let re = response.data
    showResult(re)
    return re
  },
  function(error) {
    // alert('您的网络可能有问题,请退出登录检查网络')
    // return false
    if (error.response.status === 401) {
      vv.$store.state.signed = false
      vv.$router.push('/self')
      vv.$Message.info('登录身份已过期，请从新登录~~')
      localStorage.clear()
    }
  },
)

let tokenPost = (link, setting) => {
  let s = qs.stringify(setting)
  showResult(s)
  return formTokenPost.post(link, s)
}
//=============================================================普通form表单携带Token提交👆========================================//
//=============================================================JSON字符串提交👇==================================================//
// json格式
let jToken = axios.create({
  baseURL,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
    'Accept-Language': 'zH-CN',
  },
  method: 'post',
})

// 请求拦截 塞入Token
jToken.interceptors.request.use(config => {
  if (localStorage.getItem('accessToken')) {
    config.headers.token = localStorage.getItem('accessToken')
  } else {
    this.$message.error('网络出错，请重新登录')
    this.$router.push('/')
  }
  return config
})

jToken.interceptors.response.use(
  function(response) {
    let re = response.data
    showResult(re)
    return re
  },
  function(error) {
    // alert('您的网络可能有问题,请退出登录检查网络')
    // return false
  },
)

let jtpost = (link, setting) => {
  showResult(setting)
  return jToken.post(link, setting)
}

let jtget = url => {
  return axios.get(url)
}

//=============================================================JSON字符串提交👆==================================================//
//=============================================================Get请求携带Token获取数据👇==================================================//
// form提交
let GetToken = axios.create({
  baseURL,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
    'Accept-Language': 'zH-CN',
  },
  method: 'get',
})

// 请求拦截 塞入Token
GetToken.interceptors.request.use(config => {
  if (localStorage.getItem('accessToken')) {
    config.headers.token = localStorage.getItem('accessToken')
  } else {
    this.$message.error('网络出错，请重新登录')
    this.$router.push('/')
  }
  return config
})

GetToken.interceptors.response.use(
  function(response) {
    let re = response.data
    showResult(re)
    return re
  },
  function(error) {
    // alert('您的网络可能有问题,请退出登录检查网络')
    // return false
  },
)

let gtpost = (link, setting) => {
  let s = qs.stringify(setting)
  showResult(s)
  return GetToken.get(link, s)
}

//=============================================================Get请求携带Token获取数据👆==================================================//

// 测试环境用来看请求的结果
let showResult = re => {
  // console.log(re)
}

// ===========================================================携带Token API接口👇=====================================================//
let tokenLink = {
  collection: 'my/activity/collectionByUser', // 用户收藏
  currentTime: 'my/activity/getTime', //当前系统时间
  recharge: {
    getRechargeMoney: 'my/assets/getMoney', //获取推荐充值金额
    submitRecharge: 'my/balanceRecharge', //通过支付宝提交充值金额创建订单
    subWithWeChat: 'wx/unifiedOrder', //通过微信提交充值金额创建订单
    alipay: 'unifiedOrder', //唤起支付宝支付
  },
  user: {
    info: 'my/getUserInf', //用户详细信息接口
    changeUserIcon: 'my/replaceHeadPortrait', //修改接口
    updateNick: 'my/updateNickName',//修改昵称
    tel: 'my/replacePhone', //跟换绑定手机号接口
    inPwd: 'my/replaceLoginPassword', //修改登录密码
    identity: 'my/addBankBefor', //查询用户身份
    pay: 'my/replaceTradePassword', //修改支付密码
  },
  card: {
    info: 'my/bankList', //用户银行卡列表
    keep: 'my/addBank', //保存银行卡接口
  },
  address: {
    getThreeAddress: 'my/getarea', //获取三级联动地址
    saveAddress: 'my/ygreceivingaddress', //添加收货地址
    getAddress: 'my/addressMoRen', //获取个人默认收货地址
    getAddressAll: 'my/addressAll', //获取个人收货地址
    updateAddress: 'my/findAddressOne',//修改个人收货地址
    editDefault: 'my/setDefaultAddress', //修改默认地址
    selectAddress: 'my/selectAddress',//选择收货地址
  },
  balance: 'my/assets/get', //查询用户余额
  integral: 'my/assets/findIntegralByUser', //查询用户积分
  carry: 'my/assets/integralAvailable', //转账 积分||余额
  cash: 'my/assets/putForward', //提现
  fee: 'my/assets/getshow', //用户提现 计算手续费 实际到账
  mate: {
    select: 'my/extensionRecord', // 查询推广记录
    // selectChatHis: 'my/friend/getChatRecord', //查询聊天记录
  },
  auction: {
    length: 'app/logistics/showCount', //查询所有产品记录长度
    all: 'app/logistics/showAllLogistics', //用户全部产品记录
    // current: 'my/activity/auctionRecording', //我在拍
    delivery: 'app/logistics/waitDeliver', //等待发货
    collect: 'app/logistics/alreadyDeliver', //已发货
    complete: 'app/logistics/successDeliver', //已完成
    info: 'app/logistics/details', // 获得物流信息
  },
  prxy: 'saveygmemberidentity', // 成为市代|区代
  apply: 'ygBusinesss', // 申请成为商家
  selectApply: 'getbusiness', //查询用户是否申请成为商家my/assets/putForward
  confirm: 'bidders/getJoin', //确认出价
  confirmBid: 'bidders/joinBidders', //出价
  isCollection: 'my/activity/isCollection', // 判断是否收藏
  addCollection: 'my/activity/addCollection', // 添加收藏
  cancelCollection: 'my/activity/cancelCollection', //取消收藏
  chat: {
    getCharHis: 'admin/getChat', //获取聊天记录
  },
  cart: {
    add: 'app/shop/addShoppingCart',//添加购物车
    checkList: 'app/shop/showShoppingCart',//查询购物车列表
    updateNum: 'app/shop/changeShoppingCartCount',//添加或减少购物车某件商品的数量
    delete: 'app/shop/delShoppingCart',//删除购物车商品
    empty: 'app/shop/cleanShoppingCart',//清空购物车
    goods: 'app/shop/settlementShoppingCartPage',//结算页面
    balance: 'app/shop/settlementShoppingCart',//最终结算
  },
  convertFood: 'app/exchange/exchangeGoodsExchangeBtn',//积分兑换商品
  payerInfo: 'my/assets/getReceivablesMess',//获得收款人账号信息
  rechargeNew: 'my/balanceRechargeNew',//充值
  promptlyBuy: 'app/shop/byShoppingCart',//立即购买
  voucher: 'app/showVoucher',//卡劵
}
// ===========================================================携带Token API接口👆=====================================================//

export { tokenPost, jtpost, gtpost, tokenLink, jtget }
